package com.touzhijia.scheduler.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.touzhijia.common.persistence.Page;
import com.touzhijia.scheduler.entity.QrtzTriggerLog;
import com.touzhijia.scheduler.mapper.QrtzTriggerLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 作者：LiZW <br/>
 * 创建时间：2016-8-3 23:35 <br/>
 */
@Service
@Transactional(readOnly = true)
public class TriggerLogService {

    @Autowired
    private QrtzTriggerLogMapper qrtzTriggerLogMapper;

    @Transactional(readOnly = false)
    public QrtzTriggerLog addQrtzTriggerLog(QrtzTriggerLog qrtzTriggerLog) {
        qrtzTriggerLogMapper.insertSelective(qrtzTriggerLog);
        return qrtzTriggerLog;
    }

    @Transactional(readOnly = false)
    public QrtzTriggerLog updateQrtzTriggerLog(QrtzTriggerLog qrtzTriggerLog) {
        qrtzTriggerLogMapper.updateByPrimaryKeySelective(qrtzTriggerLog);
        qrtzTriggerLog = qrtzTriggerLogMapper.selectByPrimaryKey(qrtzTriggerLog.getId());
        return qrtzTriggerLog;
    }

    /**
     * 分页查询所有的触发器日志
     *
     * @param page             分页数据
     * @param schedulerName    调度器名称
     * @param instanceName     调度器ID
     * @param triggerGroup     触发器分组
     * @param triggerName      触发器名称
     * @param jobGroup         任务分组
     * @param jobName          任务名称
     * @param jobClassName     任务实现类
     * @param startTimeByStart 开始触发时间-起始值
     * @param startTimeByEnd   开始触发时间-结束值
     * @param processTimeByMin 处理时间-最小值
     * @param processTimeByMax 处理时间-最大值
     * @return 触发器日志分页数据
     */
    public PageInfo<QrtzTriggerLog> findByPage(Page<QrtzTriggerLog> page,
                                               String schedulerName,
                                               String instanceName,
                                               String triggerGroup,
                                               String triggerName,
                                               String jobGroup,
                                               String jobName,
                                               String jobClassName,
                                               Date startTimeByStart,
                                               Date startTimeByEnd,
                                               Long processTimeByMin,
                                               Long processTimeByMax) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<QrtzTriggerLog> list = qrtzTriggerLogMapper.findByPage(
                schedulerName,
                instanceName,
                triggerGroup,
                triggerName,
                jobGroup,
                jobName,
                jobClassName,
                startTimeByStart,
                startTimeByEnd,
                processTimeByMin,
                processTimeByMax);
        return new PageInfo<>(list);
    }
}

